package com.dq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.eduservice.entity.EduCourse;
import com.dq.eduservice.mapper.EduCourseMapper;
import com.dq.eduservice.service.EduTeacherService;
import com.dq.eduservice.entity.EduTeacher;
import com.dq.eduservice.mapper.EduTeacherMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.eduservice.vo.CourseVo;
import com.dq.eduservice.vo.TeacherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 讲师(EduTeacher)表服务实现类
 *
 * @author Dq
 * @since 2022-07-14 19:53:05
 */
@Service("eduTeacherService")
public class EduTeacherServiceImpl  extends ServiceImpl<EduTeacherMapper, EduTeacher>  implements EduTeacherService {

    @Autowired
    private EduTeacherMapper teacherMapper;

    @Autowired
    private EduCourseMapper courseMapper;

    //分页查询前端
    @Override
    public Map<String, Object> getFrontTeacherList(Page<EduTeacher> page) {
        LambdaQueryWrapper<EduTeacher> eduTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduTeacherLambdaQueryWrapper.orderByDesc(EduTeacher::getId);
        Page<EduTeacher> eduTeacherPage = teacherMapper.selectPage(page, eduTeacherLambdaQueryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("items",eduTeacherPage.getRecords());
        map.put("current",eduTeacherPage.getCurrent());
        map.put("pages",eduTeacherPage.getPages());
        map.put("size",eduTeacherPage.getSize());
        map.put("total",eduTeacherPage.getTotal());
        map.put("hasNext",eduTeacherPage.hasNext());
        map.put("hasPrevious",eduTeacherPage.hasPrevious());
        return map;
    }

    //更具id获取详细信息
    @Override
    public TeacherVo getTeacherInfoById(String id) {
        EduTeacher eduTeacher = teacherMapper.selectById(id);
        TeacherVo teacherVo = new TeacherVo();
        BeanUtils.copyProperties(eduTeacher,teacherVo);
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.eq(EduCourse::getTeacherId,id);
        List<EduCourse> eduCourses = courseMapper.selectList(eduCourseLambdaQueryWrapper);
        List<CourseVo> courseVos = copyList(eduCourses);
        teacherVo.setCourseList(courseVos);
        return teacherVo;
    }


    /**
     * 集合封装
     * @param eduCourseList
     * @return
     */
    public static List<CourseVo> copyList(List<EduCourse> eduCourseList){
        List<CourseVo> courseVos = new ArrayList<>();
        for (EduCourse eduCourse : eduCourseList) {
            courseVos.add(copy(eduCourse));
        }
        return courseVos;
    }

    public static CourseVo copy(EduCourse eduCourse){
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(eduCourse,courseVo);
        return courseVo;
    }
}